import {loginApi,registerApi} from "@/api/user"

const state = {
    name: '',
    role: ''
}

const mutations = {
    SET_NAME: (state, name) => {
        state.name = name
    },
    SET_ROLE: (state, role) => {
        state.role = role
    }
}

const actions = {
    // user login
    login({ commit }, data) {
        console.log('user/login', data)
        return new Promise((resolve, reject) => {
            loginApi(data).then(res => {
              console.log('login', res);
              if (res.status === 200) {
                console.log(111);
                resolve({
                    type: "success",
                    message: res.msg,
                    data:res.data
                })
            } else {
                reject({
                    type: "error",
                    message: res.msg
                })
            }
        }).catch(error => {
            reject({
                type: "error",
                message: error.msg
            })
        })
    },  
        )
  },

  register({ commit }, data) {
    console.log('user/register', data)
    return new Promise((resolve, reject) => {
       registerApi(data).then(res => {
          console.log('register', res);
          if (res.status === 200) {
            resolve({
                type: "success",
                message: res.msg
            })
        } else {
            reject({
                type: "error",
                message: res.msg
            })
        }
    }).catch(error => {
        reject({
            type: "error",
            message: error.msg
        })
    })
},  
    )
}
}

export default {
    namespaced: true,
    state,
    mutations,
    actions
}
